const vscode = require('vscode');
const fs = require('fs');
const path = require('path');

const { registerCommand, fillComponentName, getHookUseFiel, insertComponent } = require('./util');

// 生成模板内容
const insertTemplate = (context, templateFileName) => {
	const editor = vscode.window.activeTextEditor;
	if (!editor) return vscode.window.showErrorMessage('No active editor found!');

	// 模板文件路径
	const templateFilePath = path.join(context.extensionPath, `/src/${templateFileName}`);

	fs.readFile(templateFilePath, 'utf8', (err, data) => {
		if (err) return	vscode.window.showErrorMessage(`${templateFileName}文件读取失败`);
					
		// 插入模板内容
		editor.edit(editBuilder => {
			editBuilder.insert(editor.selection.active, data);
		})
	})
}
// 生成引用
const getLinkData = () => {
	const textEditor = vscode.window.activeTextEditor
	// 获取当前文件路径和文件名
	const currentFilePath = textEditor.document.uri.fsPath;
	const fileName = path.basename(currentFilePath, path.extname(currentFilePath));

	// 计算相对于根目录的相对路径
	const workspaceFolder = vscode.workspace.getWorkspaceFolder(textEditor.document.uri);
	const srcPath = path.join( workspaceFolder.uri.fsPath, '/src');
	const relativePath = path.relative(srcPath, currentFilePath);

	// 生成 ES6 模块规范的引用路径
	const moduleImportPath = `@/${relativePath.replace(/\\/g, '/')}`;
	return {fileName, moduleImportPath}
}

/**
 * 插件激活
 * @param {vscode.ExtensionContext} context
 */
function activate(context) {
	// es6文件引用
	const writeFileToClipboard = async () => {
		const { fileName, moduleImportPath } = getLinkData();
		vscode.env.clipboard.writeText(`import ${fileName} from '${moduleImportPath}'`);
	}

	// es6变量引用
	const writeSoucresToClipboard = async () => {
		// 获取选中的文本
		const textEditor = vscode.window.activeTextEditor
		const selectedText = textEditor.document.getText(textEditor.selection);
		if (selectedText && !/\W/.test(selectedText)) {
			const { moduleImportPath } = getLinkData();
			vscode.env.clipboard.writeText(`import { ${selectedText} } from '${moduleImportPath}'`);
		}
	}

	// 生成hook使用模板
	const insertHook = async (hookFileName) => {
		const editor = vscode.window.activeTextEditor;
		let useHookFielText = await getHookUseFiel(context, hookFileName);
		if (useHookFielText && editor) {
			// 替换填充name
			useHookFielText = fillComponentName(useHookFielText);
			// 获取当前光标位置
			const position = editor.selection.active;
			editor.edit((editBuilder) => editBuilder.insert(position, useHookFielText))
		}
	}
	
	// 命令列表
	const commandsList = [
		// 键入vue3 temeplate模板
		['insertVue3_template', () => insertTemplate(context, 'vue3-template.vue')],
		// 键入vue3 vue-jsx模板
		['insertVue3_jsx', () => insertTemplate(context, 'vue3-jsx.vue')],
		// 键入vue3 jsx文件
		['insertVue3_JSX', () => insertTemplate(context, 'vue3-JSX.jsx')],
		// 生成es规范的引用
		['writeFileToClipboard', writeFileToClipboard],
		['writeSoucresToClipboard', writeSoucresToClipboard],
		// 生成常用模板
		['insertHook_useTable', () => insertHook('useTable.jsx')],
		['insertHook_useDialog', () => insertHook('useDialog.vue')],
		['insertHook_useContentWrap', () => insertHook('useContentWrap.vue')],
		['insertHook_useFrontLayoutPc', () => insertHook('useFrontLayoutPc.vue')],
		['insertHook_useH5Wrap', () => insertHook('useH5Wrap.vue')],
		// 键入常用组件
		['insert_Icon', () => insertComponent('Icon')],
		['insert_NoDataIcon', () => insertComponent('NoDataIcon')],
	]

	// 注册命令
	commandsList.forEach((v) => registerCommand(v[0], v[1], context))
}

// 插件销毁
function deactivate() {}

module.exports = {
	activate,
	deactivate
}
